package com.yhx.myDemo;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.Type;

public class ReflectTest {


    public static void main(String[] args) throws Exception{
        Student student=new Student();
        student.setAge("7");
        student.setName("lisi");
        Test test=new Test("beijing","nan");
      /*  Field field=Test.class.getField("adress");
        field.setAccessible(true);
        field.set(test,"henan");

        String value=test.adress;
        String as=(String)field.get(test);
        System.out.println("as="+as);
        System.out.println("value="+value);
        test.setAdress("hangzhou");
        test.setSex("nana");*/
        //String s=test.getTest(student,"zhangsan");
        insertObg(test, test.getClass());
    }


    public static void insertObg(Object o,Class<?> c){

       Field fields[]=c.getDeclaredFields();
       for(int i = 0, length = fields.length; i < length; i++){
           fields[i].setAccessible(true);
           String key=fields[i].getName();
           try {
               System.out.println((String)fields[i].get(o));
           }catch (Exception e){
               System.out.println("wowowowow");
           }
       }


       try{
      /*     Method method[]=c.getMethods();
           String str="";
           for(int j=0,len=method.length;j<len;j++){
               Class<?>[] parameterTypes=method[j].getParameterTypes();
               Parameter[] parameters=method[j].getParameters();
               String name=parameters[0].getName();
               System.out.println("name="+name);
               int lle=parameters.length;
               System.out.println("lle="+lle);
               int le=parameterTypes.length;
               System.out.println("le="+le);
               if("getTest".equalsIgnoreCase(method[j].getName())){
                   Student student=new Student();
                   student.setAge("7");
                   student.setName("lisi");
                   str=(String)method[j].invoke(o,student,"zhangsna");
                   System.out.println(str);
               }
           }
           System.out.println(method);
           System.out.println("class="+method[0].getDeclaringClass());
           if(o.equals(method[0].getDeclaringClass())){
               System.out.println("true");
           }*/

           Class cla = null;
           try {
               cla = Class.forName("com.yhx.myDemo.Test");//.getConstructor(classes).newInstance(args);
           } catch (Exception e) {
               e.printStackTrace();
           }
           Student student=new Student();
           student.setAge("7");
           student.setName("lisi");
           Method method=cla.getMethod("getTest",new Class[]{String.class});
           String ss=(String)method.invoke(cla.newInstance(),"fjiejfie");
           String str1=(String)cla.getClass().getMethods()[0].invoke(cla.newInstance(),student,"zhangsan11111111");
           //String str=(String)cla.getMethod("getTest").invoke(cla.newInstance(),student,"fjiejfie");

           //String str=(String)cla.getClass().getMethods()[0].invoke(o,student,"zhangsan11111111");
           System.out.println(str1);


      /*     Annotation[][] annotations=method[0].getParameterAnnotations();
           System.out.println(annotations);
           Class<?>[] parameters=method[0].getParameterTypes();
           Type[] types=method[0].getGenericParameterTypes();
           System.out.println(types);*/
       }catch (Exception e){
           System.out.println("master");
       }

    }
}
